﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;


namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCSuaHoaDonBan : UserControl
    {
        HoaDonBanBUO hdBanBUO = new HoaDonBanBUO();
        ChiTietHoaDonBanBUO chiTietHDBanBUO = new ChiTietHoaDonBanBUO();
        String idHoaDonBan,idHangHoa;
        public UCSuaHoaDonBan(String id)
        {
            InitializeComponent();
            this.idHoaDonBan = id;
            showComboboxKhachHang();
            showChiTietHoaDonBan();
            showComboboxTenHang();
            lblMaHDBan.Text = id;
            radioSua.Checked = true;
            binding();
        }
        /// <summary>
        /// Hiển thị chi tiết hóa đơn bán
        /// </summary>
        void showChiTietHoaDonBan()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = chiTietHDBanBUO.ChiTietHoaDonBan(idHoaDonBan);
        }
        /// <summary>
        /// Hiển thị danh sách hóa đơn bán
        /// </summary>
        void showDataGridView()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = hdBanBUO.ShowHoaDonBan();
        }
        /// <summary>
        /// Hiển thị combobox khách hàng
        /// </summary>
        void showComboboxKhachHang()
        {
            KhachHangBUO khachBUO=new KhachHangBUO();
            cbbKhach.DataSource = khachBUO.GetAll();
            cbbKhach.DisplayMember = "TenKhach";
            cbbKhach.ValueMember = "id_Khach";
        }
        /// <summary>
        /// Hiển thị combobox tên hàng
        /// </summary>
        void showComboboxTenHang()
        {
            HangHoaBUO hangBUO = new HangHoaBUO();
            cbbHang.DataSource = hangBUO.GetAll();
            cbbHang.DisplayMember = "TenHang";
            cbbHang.ValueMember = "id_Hang";
        }
        /// <summary>
        /// Load dữ liệu lên
        /// </summary>
        void binding()
        {
            cbbKhach.DataBindings.Clear();
            cbbKhach.DataBindings.Add("Text", dataGridView1.DataSource, "TenKhach");
            cbbHang.DataBindings.Clear();
            cbbHang.DataBindings.Add("Text", dataGridView1.DataSource, "TenHang");
            //txtSoLuong.DataBindings.Clear();
            //txtSoLuong.DataBindings.Add("Text", dataGridView1.DataSource, "SoLuong");
            dateTimePicker1.DataBindings.Clear();
            dateTimePicker1.DataBindings.Add("Text", dataGridView1.DataSource, "NgayBan");
        }
        //Tìm theo tên hàng hóa
        private void rdTimTheoTenKhach_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTenHang.Enabled = true;
            txtTimTheoMaHH.Text = "";
            txtTimTheoMaHH.Enabled = false;
            
            binding();
        }

        private void radioTimTheoNV_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            
            
        }

        private void radioAll_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTenHang.Text = "";
            txtTimTheoTenHang.Enabled = false;
            txtTimTheoMaHH.Enabled = true;
            

        }
        //tìm theo mã hàng hóa
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            dataGridView1.Enabled = true;
            
            txtTimTheoTenHang.Enabled = false;
            txtTimTheoMaHH.Enabled = true;
            
            txtTimTheoTenHang.Text = "";
            binding();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void txtTimTheoMaHH_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = hdBanBUO.SearchHangHoaTrongChiTietHDBanByIDHangHoa(txtTimTheoMaHH.Text.Trim());
            binding();
        }

        private void txtTimTheoTenHang_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = hdBanBUO.SearchHangHoaTrongChiTietHDBanByTenHang(txtTimTheoTenHang.Text);
            binding();
        }

        private void radioSua_CheckedChanged(object sender, EventArgs e)
        {
            btnThem.Enabled = false;
            btnSua.Enabled = true;
            txtTimTheoMaHH.Enabled = true;
            txtTimTheoTenHang.Enabled = true;
            binding();
        }

        private void radioThem_CheckedChanged(object sender, EventArgs e)
        {
            btnThem.Enabled = true;
            btnSua.Enabled = false;
            cbbKhach.Enabled = false;
            txtTimTheoMaHH.Enabled = false;
            txtTimTheoTenHang.Enabled = false;
        }

        private void xóaHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChiTietHoaDonBanBUO chiTietHoaDonBanBUO = new ChiTietHoaDonBanBUO();
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (chiTietHoaDonBanBUO.Delete(idHangHoa,idHoaDonBan))
                {
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                showChiTietHoaDonBan();
            }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        idHoaDonBan = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();
                        idHangHoa = dataGridView1.Rows[hit.RowIndex].Cells[1].Value.ToString();
                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }
        //String sl;
        private void btnSua_Click(object sender, EventArgs e)
        {
            ChiTietHoaDonBanBUO chiTietHDBanBUO = new ChiTietHoaDonBanBUO();
            tbl_Hang tblHang = (tbl_Hang)cbbHang.SelectedItem;

            string id = tblHang.id_Hang.ToString();
            tbl_HoaDonBan_Hang obj=new tbl_HoaDonBan_Hang();
            //obj.id_Hang = id;
            obj.SoLuong=int.Parse(txtSoLuong.Text);
            //obj.SoLuong = 9;
            if (chiTietHDBanBUO.Update(idHoaDonBan, id, obj))
            {
                dataGridView1.DataSource = chiTietHDBanBUO.ChiTietHoaDonBan(idHoaDonBan);
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                dataGridView1.DataSource = chiTietHDBanBUO.ChiTietHoaDonBan(idHoaDonBan);
                MessageBox.Show("Cập nhật dữ liệu không thành công \n Có thể dữ liệu bị trùng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            binding();
        }

        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            //sl = txtSoLuong.Text;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                tbl_HoaDonBan_Hang obj = new tbl_HoaDonBan_Hang();
                tbl_Hang t = (tbl_Hang)cbbHang.SelectedItem;
                obj.id_HoaDonBan = idHoaDonBan;
                obj.id_Hang = t.id_Hang;
                obj.SoLuong = int.Parse(txtSoLuong.Text);
                if (chiTietHDBanBUO.Insert(obj))
                {
                    showChiTietHoaDonBan();
                    MessageBox.Show("Thêm dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadLaiFormSua();
                    radioThem.Checked = true;
                    radioSua.Checked = false;
                    cbbKhach.Enabled = false;
                }
                else
                {
                    showChiTietHoaDonBan();
                    MessageBox.Show("Thêm dữ liệu không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Thêm dữ liệu không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        void LoadLaiFormSua()
        {
            Form form = this.ParentForm;
            UCSuaHoaDonBan sua=new UCSuaHoaDonBan(idHoaDonBan);
            ControlAccess.ShowConTrol(form, sua, "grbHienThiChucNang");
            
        }
    }
}
